package com.thinkit.bigdata.web.controller.config;

import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.core.util.StringUtil;
import com.thinkit.bigdata.web.model.config.SysStatusAtt;
import com.thinkit.bigdata.web.model.config.SysStatusAttExample;
import com.thinkit.bigdata.web.model.result.ConfigResult;
import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.service.config.StatusAttService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by lihongli on 2018/8/31
 */
@Controller
@RequestMapping("statusAtt")
public class StatusAttController {

    private static Logger logger = Logger.getLogger(StatusAttController.class);

    @Resource
    private StatusAttService statusAttService;

    /**
     * 请求进入属性状态字段配置页
     *
     * @return
     */
    @RequestMapping(value = "/statusAtts", method = RequestMethod.GET)
    public String showStatusAtts(Model model, String type, String name) {
        logger.info("进入属性状态字段配置页");
        model.addAttribute("type", type);
        model.addAttribute("name", name);
        return "ConfigViews/statusAtt";
    }

    /**
     * 根据传入的type获取该type下所有属性状态字段信息
     * @param request
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/statusAttsGetByType")
    public List<SysStatusAtt> statusAttsGetByType(HttpServletRequest request, @RequestParam("type") String type){
        SysStatusAttExample example = new SysStatusAttExample();
        if (StringUtil.isNotEmpty(type)) {
            example.createCriteria().andTypeEqualTo(type);
        }
        return statusAttService.selectByExample(example);
    }

    /**
     * 分页获取数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/statusAttByPage")
    public Page<SysStatusAtt> statusConfigByPage(
            HttpServletRequest request,
            @RequestParam(value = "sortOrder") String sortOrder,
            @RequestParam(value = "sortName") String sortName,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        String type = request.getParameter("type");
        String logTxt = "【属性状态配置分页查询】";
        logger.info(logTxt + "查询条件: pageNum=" + pageNum);
        logger.info(logTxt + "查询条件: pageSize=" + pageSize);
        logger.info(logTxt + "查询条件: sortOrder=" + sortOrder);
        logger.info(logTxt + "查询条件: sortName=" + sortName);
        logger.info(logTxt + "查询条件: type=" + type);
        Page<SysStatusAtt> page = new Page<>(pageNum, pageSize);
        SysStatusAttExample example = new SysStatusAttExample();
        if (StringUtil.isNotEmpty(type)) {
            example.createCriteria().andTypeEqualTo(type);
        }
        example.setOrderByClause(sortName + " " + sortOrder);
        statusAttService.selectByExample(page, example);
        logger.info(logTxt + "共查询出" + page.getResult().size() + "条属性状态信息");
        return page;
    }

    /**
     * 新增字段前检查字段中的name是否存在
     *
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/attNameUniqueCheck")
    public ConfigResult attNameUniqueCheck(HttpServletRequest request, @RequestParam(value = "name") String name, @RequestParam(value = "type") String type) {
        String logTxt = "【属性状态字段唯一性验证】";
        logger.info(logTxt + "name=" + name);
        SysStatusAttExample example = new SysStatusAttExample();
        example.createCriteria().andNameEqualTo(name).andTypeEqualTo(type);
        int count = statusAttService.countByExample(example);
        if (count > 0) {
            logger.info(logTxt + name + "已存在");
            return new ConfigResult(Constants.ERROR, "已存在");
        } else {
            logger.info(logTxt + name + "不存在，校验通过");
            return new ConfigResult(Constants.SUCCESS, "√");
        }
    }

    /**
     * 新增字段前检查字段中的value是否存在
     *
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/attValueUniqueCheck")
    public ConfigResult attValueUniqueCheck(HttpServletRequest request, @RequestParam(value = "value") String value, @RequestParam(value = "type") String type) {
        String logTxt = "【属性状态字段唯一性验证】";
        logger.info(logTxt + "value=" + value);
        SysStatusAttExample example = new SysStatusAttExample();
        example.createCriteria().andValueEqualTo(value).andTypeEqualTo(type);
        int count = statusAttService.countByExample(example);
        if (count > 0) {
            logger.info(logTxt + value + "已存在");
            return new ConfigResult(Constants.ERROR, "已存在");
        } else {
            logger.info(logTxt + value + "不存在，校验通过");
            return new ConfigResult(Constants.SUCCESS, "√");
        }
    }

    /**
     * 修改之前根据主键获取属性状态字段信息
     *
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusAttGetByPK", method = RequestMethod.POST)
    public SysStatusAtt statusAttGetByPK(HttpServletRequest request, @RequestParam("id") Long id) {
        logger.info("获取id为【" + id + "】的属性状态字段信息");
        return statusAttService.statusAttGetByPK(id);
    }

    /**
     * 添加属性状态字段信息
     *
     * @param request
     * @param sysStatusAtt
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusAttAdd", method = RequestMethod.POST)
    public ConfigResult statusAttAdd(HttpServletRequest request, SysStatusAtt sysStatusAtt) {
        String logTxt = "【属性状态字段添加方法】";
        logger.info(logTxt + "添加" + sysStatusAtt.toString());
        // 设置写入时间
        sysStatusAtt.setLastupdatetime(new Date());
        try {
            int rowNum = statusAttService.insert(sysStatusAtt);
            if (rowNum > 0) {
                logger.info(logTxt + "成功插入" + rowNum + "条属性状态字段信息");
                return new ConfigResult(Constants.SUCCESS, "属性状态字段添加成功");
            } else {
                logger.info(logTxt + "属性状态字段信息添加失败");
                return new ConfigResult(Constants.FAIL, "属性状态字段添加失败");
            }
        } catch (Exception e) {
            logger.info(logTxt + "属性状态字段信息添加错误", e);
            return new ConfigResult(Constants.ERROR, "属性状态字段信息添加错误");
        }
    }

    /**
     * 修改属性状态字段信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusAttUpdate", method = RequestMethod.POST)
    public ConfigResult statusAttUpdate(HttpServletRequest request, SysStatusAtt statusAtt) {
        String logTxt = "【属性状态字段修改方法】";
        logger.info(logTxt + "修改" + statusAtt.toString());
        try {
            statusAtt.setLastupdatetime(new Date());
            int rowNum = statusAttService.update(statusAtt);
            if (rowNum > 0) {
                logger.info(logTxt + "成功更新" + rowNum + "条属性状态字段信息");
                return new ConfigResult(Constants.SUCCESS, "属性状态字段更新成功");
            } else {
                logger.info(logTxt + "属性状态字段信息更新失败");
                return new ConfigResult(Constants.FAIL, "属性状态字段更新失败");
            }
        } catch (Exception e) {
            logger.info(logTxt + "属性状态字段信息更新错误", e);
            return new ConfigResult(Constants.ERROR, "属性状态字段信息更新错误");
        }
    }

    /**
     * 删除属性状态字段信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusAttDelete", method = RequestMethod.POST)
    public ConfigResult statusAttDelete(HttpServletRequest request, @RequestParam("id") Long id) {
        String logTxt = "【属性状态字段删除方法】";
        logger.info(logTxt + "删除id为" + id + "的属性状态字段");
        try {
            int rowNum = statusAttService.delete(id);
            if (rowNum > 0) {
                logger.info(logTxt + "成功删除属性状态字段信息" + rowNum + "条");
                return new ConfigResult(Constants.SUCCESS, "属性状态字段删除成功");
            } else {
                logger.info(logTxt + "属性状态字段信息更新失败");
                return new ConfigResult(Constants.FAIL, "属性状态字段删除失败");
            }
        } catch (Exception e) {
            logger.info(logTxt + "属性状态字段信息删除错误", e);
            return new ConfigResult(Constants.ERROR, "属性状态字段信息删除错误");
        }
    }
}
